package com.sikaryofficial.backend.utils;

import org.jasypt.util.text.AES256TextEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;

/**
 * @author : qinjinyuan
 * @desc : jasypt 加解密
 * @date : 2024/04/01 14:56
 */
public class JasyptUtil {

    public static void main(String[] args) {
        f1_BasicTextEncryptor();
        // f2_AES256TextEncryptor();
    }

    /**
     * 加密工具类: org.jasypt.util.text.BasicTextEncryptor
     * 加密算法: PBEWithMD5AndDES
     */
    public static void f1_BasicTextEncryptor() {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        System.out.println("当前加密方式: 加密类: BasicTextEncryptor, 加密算法: PBEWithMD5AndDES ");
        // 1.设置秘钥
        String salt = "J8YLoserSKE2024";
        textEncryptor.setPassword(salt);
        // 2.加密
        // 2.1加密内容
        String pd = "your pass word";
        System.out.println("加密前:  " + pd);
        // 2.2加密操作
        String pdAfterEncrypt = textEncryptor.encrypt(pd);
        System.out.println("加密后:  " + pdAfterEncrypt);
        // 3.解密操作
        String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
        System.out.println("解密后:  " + pdAfterDecrypt);
    }

    /**
     * 加密工具类: org.jasypt.util.text.AES256TextEncryptor
     * 加密算法: PBEWithHMACSHA512AndAES_256
     */
    public static void f2_AES256TextEncryptor() {
        AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
        System.out.println("当前加密方式: 加密类: AES256TextEncryptor, 加密算法: PBEWithHMACSHA512AndAES_256 ");
        // 1.设置秘钥
        String salt = "J8YLoserSKE2024";
        textEncryptor.setPassword(salt);
        // 2.加密
        // 2.1加密内容
        String pd = "xxxxxx";
        System.out.println("加密前:  " + pd);
        // 2.2加密操作
        String pdAfterEncrypt = textEncryptor.encrypt(pd);
        System.out.println("加密后:  " + pdAfterEncrypt);
        // 3.解密操作
        String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
        System.out.println("解密后:  " + pdAfterDecrypt);

    }
}
